package myproject;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserRepository1 implements Serializable {
	private String jdbcUrl = "jdbc:mysql://120.79.250.60:3306/wrf_cg";
	private String username = "root";
	private String password ="123456";
	public UserRepository1() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public List<User1> getUser(){
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		SQLException exception = null;
		List<User1> users =null;
		try {
			connection = DriverManager.getConnection(jdbcUrl, username, password);
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM sj2");
			users = new ArrayList<User1>();
			while(resultSet.next()) {
				User1 user=new User1();
				user.setSpid(resultSet.getString(1));
				user.setSpname(resultSet.getString(2));
				user.setSpprice(resultSet.getInt(3));
				users.add(user);
			}
		} catch (SQLException e) {
			exception=e;
		} finally {
			if(statement!=null) {
				try {
					statement.close();
				}
				catch(SQLException e) {
					if(exception!=null) {
						exception = e;
					}
				}
			}
			if(connection!=null) {
				try {
					connection.close();
				}
				catch(SQLException e) {
					if(exception!=null) {
						exception = e;
					}
				}
			}
			if(exception!=null) {
				throw new RuntimeException(exception);
			}
		}
		return users;
	}
}
